package team.bailan.entity.domain;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.bailan.common.enums.CardTypeEnum;
import team.bailan.common.process.dealcard.DealCards;
import team.bailan.common.process.dealcard.InitialCards;
import team.bailan.common.process.ingame.InGame;
import team.bailan.common.process.ingame.InGameMap;
import team.bailan.common.server.WebSocketServer;
import team.bailan.common.util.SpringUtil;
import team.bailan.common.util.TokenUtil;
import team.bailan.controller.user.UserController;
import team.bailan.entity.vo.BeanInfVO;
import team.bailan.entity.vo.PrepareInfVO;
import team.bailan.mapper.UserMapper;
import team.bailan.service.UserService;
import team.bailan.service.impl.UserServiceImpl;

import java.io.IOException;
import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RoomInf {
    int roomId;
    Boolean[] positionFlag = {false, false, false, false};
    Boolean[] prepareFlag = {false, false, false, false};
    String[] usernameList = new String[4];
    Integer[] userBeans = new Integer[4];
    FractionCalculator fractionCalculator;


    public void init() {
        prepareFlag[0] = false;
        prepareFlag[1] = false;
        prepareFlag[2] = false;
        prepareFlag[3] = false;
        fractionCalculator.init();
    }

    public boolean judgeFreePosition() {
        for (int i = 1; i <= 3; i++) {
            if (!positionFlag[i]) {
                return true;
            }
        }
        return false;
    }

    public int enterTheRoom(String username, int beans) {
        for (int i = 1; i <= 3; i++) {
            if (!positionFlag[i]) {
                positionFlag[i] = true;
                usernameList[i] = username;
                userBeans[i] = beans;
                return i;
            }
        }
        return 0;
    }

    public void leaveTheRoom(int playerPosition) {
        positionFlag[playerPosition] = false;
        usernameList[playerPosition] = null;
        userBeans[playerPosition] = null;
    }

    public void prepareForGame(int playerPosition) throws IOException {
        prepareFlag[playerPosition] = true;
        PrepareInfVO prepareInfVO = new PrepareInfVO();
        prepareInfVO.setMsgType("PrepareInf");
        prepareInfVO.setData(this);
        prepareInfVO.setPlayerPosition(playerPosition);
        boolean isStart = prepareFlag[1] && prepareFlag[2] && prepareFlag[3];
        prepareInfVO.setStartFlag(isStart);
        if (isStart) {
            InGame inGame = new InGame();
            InGameMap.inGameMap.put("room_" + roomId, inGame);
        }
        if (InGameMap.inGameMap.containsKey("room_" + roomId)) {
            InGameMap.inGameMap.get("room_" + roomId).setStart(isStart);
        }
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(prepareInfVO), roomId + "");
    }

    public void calculateBeans(String winner, int lordPosition) throws IOException {
        this.getFractionCalculator().calculateBeans(winner, lordPosition, this.getUserBeans());
        SpringUtil.getBean(UserService.class).updateBeans(usernameList, userBeans);
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(new BeanInfVO("BeansChange", userBeans)), roomId + "");
    }

    @Override
    public String toString() {
        return "RoomInf{" +
                "roomId=" + roomId +
                ", positionFlag=" + Arrays.toString(positionFlag) +
                ", prepareFlag=" + Arrays.toString(prepareFlag) +
                ", usernameList=" + Arrays.toString(usernameList) +
                '}';
    }


}
